package com.ttc.mBackup.BackupAgent;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import android.app.AlertDialog;
import android.os.Environment;
import android.widget.Toast;

import com.dropbox.client.DropboxAPI;
import com.dropbox.client.DropboxAPI.FileDownload;

public class DropBoxPerform {

	private DropboxAPI api;
	private String name;
	
	public DropBoxPerform(DropboxAPI api, String name)
	{
		this.api=api;
		this.name=name;
	}
	
	public boolean uploadfile()
	{
    	/*Upload and Create Folder*/
    
			File f= new File("data/data/com.ttc.mBackup/databases"
	        		+File.separator
	      		   +"BACKUPSYSTEM");
           if (!f.exists()) 
           {
            //File does not exists
            
           }
           File f1=  new File("data/data/com.ttc.mBackup/databases"
        			+File.separator
        			   +name);
f.renameTo(f1);
	
     api.putFile("dropbox", "/BackupSystems", f1);
     f.delete();
     f1.delete();
		return true;
	}
	
	public boolean downloadfile()
	{
		FileDownload fd = api.getFileStream("dropbox","/BackupSystems"+File.separator+name, null);
		File dir = new File("data/data/com.ttc.mBackup/databases");
		if(!dir.exists())
		{
			dir.mkdirs();
		}
        File f1=new File("data/data/com.ttc.mBackup/databases"
        		+File.separator
     		   +"BACKUPSYSTEM");
        
          OutputStream out;
		try {
			out = new FileOutputStream(f1);
		

        byte buf[]=new byte[1024];

        int len;

        while((len=fd.is.read(buf))>0)

        out.write(buf,0,len);

        out.close();

        fd.is.close();
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      ///  f1.renameTo(f2);
        return true;
	}
    
}
